package logic

import (
	"context"
	"fmt"

	"rpc/wfw1/rpc/internal/svc"
	"rpc/wfw1/rpc/models"
	"rpc/wfw1/rpc/returnage"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

type Userq struct {
	Name string `json:"name"`
	Age  string `json:"age"`
}

func NewGetageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetageLogic {
	return &GetageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 定义getage rpc方法

// gorm配置
func (l *GetageLogic) Getage(in *returnage.GetageReq) (*returnage.GetageResp, error) {
	// todo: add your logic here and delete this line

	var userq Userq
	result := models.GormDb.Table("user").Where("name = ?", in.Name).Take(&userq)

	if result.Error != nil {
		fmt.Println("查询失败:", result.Error)
		return nil, result.Error
	}

	return &returnage.GetageResp{
		Age: userq.Age,
	}, nil
}

// // redis配置
// func (l *GetageLogic) Getage(in *returnage.GetageReq) (*returnage.GetageResp, error) {
// 	// todo: add your logic here and delete this line

// 	val, err := models.GetKey(in.Name)
// 	if err != nil {
// 		fmt.Println("获取键失败:", err)
// 		return nil, err
// 	}
// 	//fmt.Println(val)

// 	return &returnage.GetageResp{
// 		Age: val,
// 	}, nil
// }
